<template>
  <div class="flex flex-col h-full">
    <SearchContainer show-export @search="getData" @reset="() => formRef.resetFields()" @export="exportRef.$el.querySelector('button').click()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="挂号日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="docker" label="医生">
          <SelectTable :value="formState.empName" class-name="w-[240px]" type="医生" @select="onSelectDoctor" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="flex-1">
      <a-space ref="exportRef" class="hidden">
        <BaseExport type="报表" name="门诊医生工作量统计" code="MAP000442" is-show-time :search-params="formState">导出</BaseExport>
      </a-space>
      <BaseTable v-if="columns.length" :scroll="{ x: columns.length * 130 }" :loading="loading" :data-source="tableData" fill :columns="columns">
        <!-- <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
        </template> -->
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const exportRef = ref(null)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().subtract(30, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const columns = ref([])
const { loading, tableData, getData } = useTable({
  url: '/statistic/outpWorkloadStatistics',
  params: formState.value,
  pagination: false,
  immediate: true,
  onSuccess: (res) => {
    columns.value = Object.keys(res[0]).map((item) => {
      const align = typeof res[0][item] === 'number' ? 'right' : 'left'
      const customRender = item.indexOf('金额') > -1 || item.indexOf('费') > -1 ? ({ text }) => formatAmount(text, 2) : null
      return { title: item, dataIndex: item, customRender, align, ellipsis: true }
    })
  }
})

const onSelectDoctor = (select) => {
  formState.value.zjm = select.empId
  formState.value.empName = select.empName
}
</script>
